/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Base for creating a custom VGUI class; remove the functions you don't use.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

require( "datastream" )

local PANEL = {}

local Selected = ""
local Received = false
MAPS = {}

function PANEL:Init()
	//Create controls here
	
	
	self:SetTitle( " " )
	self:ShowCloseButton( false )
	

	local MapList = vgui.Create( "DPanelList", self )
		MapList:EnableHorizontal( true )
		MapList:EnableVerticalScrollbar( true )
		MapList:SetPadding( 2 )
		MapList:SetSpacing( 4 )
		
		self.MapList = MapList
		
		RunConsoleCommand( "StreamMaps" )
	//self:AddListItems()
	
	local ChangeMap = vgui.Create( "DButton", self )
	local SP = self
		ChangeMap:SetText( "Change Map" )
		function ChangeMap:DoClick()
			if Selected != "" then
				local TimerMenu = vgui.Create( "DMenu", ChangeMap )
					TimerMenu:AddOption( "Now", function() RunConsoleCommand( "DoChangeLevel", Selected ) end )
					TimerMenu:AddOption( "In 10 Seconds", function()
					if timer.IsTimer( "MCHANGE" ) then
						timer.Destroy( "MCHANGE" )
						RunConsoleCommand( "ShowMapTimer", "", CurTime(), "REMOVE" )
					end
														RunConsoleCommand( "ShowMapTimer", Selected, math.ceil(CurTime()) + 10 )
														timer.Create( "MCHANGE", 10, 1, function() RunConsoleCommand( "DoChangeLevel", Selected ) SP:Remove() end ) 
														end )
					TimerMenu:AddOption( "In 30 Seconds", function()
					if timer.IsTimer( "MCHANGE" ) then
						timer.Destroy( "MCHANGE" )
						RunConsoleCommand( "ShowMapTimer", "", CurTime(), "REMOVE" )
					end
														RunConsoleCommand( "ShowMapTimer", Selected, math.ceil(CurTime()) + 30 )
														timer.Create( "MCHANGE", 30, 1, function() RunConsoleCommand( "DoChangeLevel", Selected ) SP:Remove() end ) 
														end )
					TimerMenu:AddOption( "In 1 Minute", function()
					if timer.IsTimer( "MCHANGE" ) then
						timer.Destroy( "MCHANGE" )
						RunConsoleCommand( "ShowMapTimer", "", CurTime(), "REMOVE" )
					end
														RunConsoleCommand( "ShowMapTimer", Selected, math.ceil(CurTime()) + 60 )
														timer.Create( "MCHANGE", 60, 1, function() RunConsoleCommand( "DoChangeLevel", Selected ) SP:Remove() end ) 
														end )
					TimerMenu:AddOption( "In 5 Minutes", function()
					if timer.IsTimer( "MCHANGE" ) then
						timer.Destroy( "MCHANGE" )
						RunConsoleCommand( "ShowMapTimer", "", CurTime(), "REMOVE" )
					end
														RunConsoleCommand( "ShowMapTimer", Selected, math.ceil(CurTime()) + 300 )
														timer.Create( "MCHANGE", 300, 1,function() RunConsoleCommand( "DoChangeLevel", Selected ) SP:Remove() end ) 
														end )
					TimerMenu:AddOption( "In 10 Minutes", function()
					if timer.IsTimer( "MCHANGE" ) then
						timer.Destroy( "MCHANGE" )
						RunConsoleCommand( "ShowMapTimer", "", CurTime(), "REMOVE" )
					end
														RunConsoleCommand( "ShowMapTimer", Selected, math.ceil(CurTime()) + 600 )
														timer.Create( "MCHANGE", 600, 1,function() RunConsoleCommand( "DoChangeLevel", Selected ) SP:Remove() end ) 
														end )
					TimerMenu:AddOption( "Close Menu", function() TimerMenu:Remove() end )
					TimerMenu:Open()
			end
		end
		
		self.ChangeMap = ChangeMap
		
		
		local CancelMapChange = vgui.Create( "DButton", self )
			CancelMapChange:SetText( "Cancel Map Change" )
			function CancelMapChange:DoClick()
				if timer.IsTimer( "MCHANGE" ) then
					timer.Destroy( "MCHANGE" )
					RunConsoleCommand( "ShowMapTimer", "", CurTime(), "REMOVE" )
				end
			end
			
			self.CancelMapChange = CancelMapChange
		
		
		local Exit = vgui.Create( "DButton", self )
			Exit:SetText( "Exit" )
			function Exit:DoClick()
				SP:Remove()
				CanMCHANGE = false
				Received = false
				MAPS = {}
			end
			
			self.Exit = Exit
			
		self:SetSize( ScrW() * 0.6, ScrH() * 0.75 )
		self:Center()
		self:MakePopup()
	
end

function MAPSTREAMHOOK( ply, handle, id, encoded, decoded )
	MAPS = encoded[1]
end
datastream.Hook( "MAPSTREAM", MAPSTREAMHOOK )

function PANEL:AddListItems( maps )
	
	for k,v in pairs( maps ) do
	local IsHighlighted = false
	local MAP = string.Replace( v, ".bsp", "" )
		
		local MapP = vgui.Create( "DPanel", self.MapList )
			MapP:SetSize( ScrW() * 0.25, ScrH() * 0.2 )
			function MapP:Paint()
				draw.RoundedBox( 2, 4, 4, self:GetWide() - 8, self:GetTall() - 8, Color( 0, 0, 0, 120 ) )
			end
			
		local MapT = vgui.Create( "DLabel", MapP )
			MapT:SetText( MAP )
			MapT:SizeToContents()
			MapT:Center()
			
		local MapC = vgui.Create( "DPanel", MapP )
			MapC:StretchToParent( 0, 0, 0, 0 )
			function MapC:OnCursorEntered() IsHighlighted = true end
			function MapC:OnCursorExited() IsHighlighted = false end
			function MapC:OnMousePressed() Selected = MAP end
			function MapC:Paint()
				
				if IsHighlighted then
					surface.SetDrawColor( Color( 40, 40, 255, 255 ) )
					surface.DrawOutlinedRect( 4, 4, self:GetWide() - 8, self:GetTall() - 8 )
				end
				
				if Selected == MAP then
					surface.SetDrawColor( Color( 0, 255, 0, 50 ) )
					surface.DrawRect( 0, 0, self:GetWide(), self:GetTall() )
				end
				
			end
			
		self.MapList:AddItem( MapP )
		
	end
end

function PANEL:PerformLayout()
	
	// Let's make sure we perform the layout of the class this panel is based on here as well.
	
	self.MapList:StretchToParent( 2, 2, 2, self:GetTall() * 0.25 )
	
	self.ChangeMap:SetSize( 75, 25 )
	self.ChangeMap:Center()
	self.ChangeMap:AlignBottom( 4 )
	
	self.CancelMapChange:SetSize( 125, 25 )
	self.CancelMapChange:AlignBottom( 4 )
	self.CancelMapChange:MoveLeftOf( self.ChangeMap, 5 )
	
	self.Exit:SetSize( 75, 25 )
	self.Exit:AlignBottom( 4 )
	self.Exit:MoveRightOf( self.ChangeMap, 5 )

end

function PANEL:Paint()
	//Do Painting here
	
	draw.RoundedBox( 2, 0, 0, self:GetWide(), self:GetTall(), Color( 40, 40, 40, 255 ) )
	
	function self.MapList:Paint()
		surface.SetDrawColor( Color( 255, 255, 255, 50 ) )
		surface.DrawRect( 0, 0, self:GetWide(), self:GetTall() )
		
		surface.SetDrawColor( Color( 255, 255, 255, 50 ) )
		surface.DrawOutlinedRect( 0, 0, self:GetWide(), self:GetTall() )
	end

end

function PANEL:PaintOver()

	//Paint over self and child elements here.

end

function PANEL:Think()
	//Think stuff here
	
	if CanMCHANGE and !Received then
		self:AddListItems( MAPS )
		Received = true
	end

end

function PANEL:OnCursorEntered()

	//Do stuff when the cursor enters the panel

end

function PANEL:OnCursorExited()

	//Do Stuff when the cursor leaves the panel

end

function PANEL:OnMouseWheeled()

	//Do stuff when you wheel the mouse on this panel

end

function PANEL:OnMousePressed()

	//Do stuff when you press the mouse on this panel

end

function PANEL:OnMouseReleased()

	//Do stuff when you release the mouse on this panel

end

function PANEL:OnKeyCodePressed()

	//Do stuff when using keys from the keyboard on this panel. This hook uses the keycode enumerations
	//http://wiki.garrysmod.com/?title=P.OnKeyCodePressed	For more information

end

function PANEL:ActionSignal()

	//This is called on the parent frame when a child VGUI element is used. The key corresponds to what is set by Panel.SetCommand.
	//From the wiki
	
end

function PANEL:OnCursorMoved()

	//From the wiki
	//Called whenever the the mouse is moved within the panel.

end

function PANEL:DoClick()

	//For button type panels, use this. Called when you click the button.

end


//Register syntax - Desired element name, Panel to use, base class of the new element.
vgui.Register( "MapMenu", PANEL, "DFrame" )